package com.rfid.base.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class DateUtils {

    public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String PARAM_DATE_FORMAT = "yyyy-MM-dd";

    public static String date2String(Date date, String format, Locale locale) {
        if (date == null) {
            return null;
        }
        return new SimpleDateFormat(format, locale).format(date);
    }

    public static String date2String(Date date, String format) {
        return date2String(date, format, Locale.getDefault());
    }

    public static String date2DefaultString(Date date) {
        return date2String(date, DEFAULT_DATE_FORMAT);
    }

    /**
     * 获取当前日期是星期几<br>
     *
     * @param date
     * @return 当前日期是星期几
     */
    public static int getWeekOfDate(Date date) {
        int[] weekDays = {7, 1, 2, 3, 4, 5, 6};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    /**
     * @return 获取今天起始时间
     */
    public static long getTodayStartTime() {
        Calendar todayStart = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        return todayStart.getTime().getTime();
    }

    /**
     * @return 获取今天结束时间
     */
    public static long getTodayEndTime() {
        Calendar todayEnd = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        return todayEnd.getTime().getTime();
    }


    public static long getTime(int hour, int minute, int second) {
        Calendar todayEnd = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
        todayEnd.set(Calendar.HOUR_OF_DAY, hour);
        todayEnd.set(Calendar.MINUTE, minute);
        todayEnd.set(Calendar.SECOND, second);
        todayEnd.set(Calendar.MILLISECOND, 0);
        return todayEnd.getTime().getTime();
    }

    public static String getDateStr(long time, String format) {
        SimpleDateFormat sd = new SimpleDateFormat(format);
        Date date = new Date(time);
        return sd.format(date);
    }
}
